package guestbook;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@PersistenceContext
public class SignGuestbookServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2800778962458456034L;

	private static final Logger log = Logger
			.getLogger(SignGuestbookServlet.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		String content = req.getParameter("content");
		Date date = new Date();
		
		
		try {
			EntityUser entityUser = new EntityUser();
			entityUser.setUser(user);
			entityUser.setContent(content);
			entityUser.setDate(date);
			
			entityUser.save();
		} catch (Exception e) {
			log.log(Level.WARNING, "doPost() : "+e);
		}

		
		resp.sendRedirect("/guestbook.jsp");
		
	}
}